package com.rocky.study.serial;

import java.util.concurrent.atomic.AtomicInteger;

public class SerialPrintDemo {

    static volatile  int flag = -1;
    static AtomicInteger NUM = new AtomicInteger(100);
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            int finalI = i;
            new Thread(() -> {
                while(true) {
                    if (flag == finalI - 1) {
                        System.out.println(Thread.currentThread().getName() + "处理:" + NUM.getAndIncrement());
                        // 线程通信 把下一个线程 给赋予上
                        flag = finalI;
                        break;
                    }
                }
            },"t"+i).start();
        }
    }
}
